
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:tp_upgrade_lib/tp_update_widget.dart';
import 'package:tp_upgrade_lib/tp_upgrade_lib.dart';

class TpUpdateUtil{

  static checkVs(BuildContext context) async{
    Map<String,dynamic> resultData = {
      "buildBuildVersion": "9",
      "forceUpdateVersion": "",
      "forceUpdateVersionNo": "",
      "needForceUpdate": true,
      // "downloadURL": "https://www.xcxwo.com/app/installUpdate/b1dc8c0873d414aa171a6fb81479f1aa?sig=OGZnhfoM1HOOFSiNNu34aGSuAyD9ozdycRVAa6G11irNYEcvLZ9Aw0ccnLAnPVuv&forceHttps=",
      // "downloadURL": "https://www.pgyer.com/app/installUpdate/cb2505cdeea0415aba55e901f7acb939?sig=dSq4q45kkwvzeKsZy3Pep5MAY1wMR4gWWAzAxNM6UPb%2BUZFS8wIjV72dldxXRrKV&forceHttps=",
      // "downloadURL": "https://apd.lmdo.cn/admin/sys-file/oss/file?fileName=33e4f87a691b4e2e939956aee1bf0a01.apk",
      "downloadURL": "http://106.14.147.199:9000/factorydy/2025/4/2/dc40a81df92f4c55ad841676cf881edb.apk?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20250422T134025Z&X-Amz-SignedHeaders=host&X-Amz-Expires=604800&X-Amz-Credential=hPafKoaGwLDUfLkrI4pI%2F20250422%2F%2Fs3%2Faws4_request&X-Amz-Signature=7c50271ba796c64eca8a7a90addcd670c7f7b078457c2478ade2b4c893496d7c",
      "buildHaveNewVersion": false,
      "buildVersionNo": "76",
      "buildVersion": "2.1.6",
      "buildDescription": "管理端！",
      "buildUpdateDescription": "优化部分功能",
      "appKey": "c8b5dddb89dac5fe06552f79ff37bdba",
      "buildKey": "9d79325cb8fe0c3f26b7a0f6657551ec",
      "buildName": "管理端",
      "buildIcon": "https://cdn-app-icon.pgyer.com/5/3/8/2/a/5382a83da897d663b4d3e228dd524ebb?x-oss-process=image/resize,m_lfit,h_120,w_120/format,jpg",
      "buildFileKey": "5aa78e46d45e06c07782bdde52f0eb6a.apk",
      "buildFileSize": "51247860"
    };
    if(resultData!=null){
      print('response-->resultData is Map=${resultData is Map}');
      print('response-->resultData =${resultData}');
      // bool needForceUpdate = resultData['needForceUpdate'];
      bool needForceUpdate = false;
      String buildVersionNo = resultData['buildVersionNo'];
      String buildVersion = resultData['buildVersion'];
      String buildUpdateDescription = resultData['buildUpdateDescription'];
      String buildName = resultData['buildName'];
      String buildFileSize = resultData['buildFileSize'];
      String downloadURL = resultData['downloadURL'];
      String _buildNumber=await _getAppInfo();
      if((double.parse(_buildNumber))>=double.parse(buildVersionNo))return;
      _popDialog(context,needForceUpdate,buildVersionNo,buildVersion,buildUpdateDescription,buildName,buildFileSize,downloadURL);
    }
  }

  static Future<String> _getAppInfo() async{
    return '1';
  }
 static void _popDialog(
      BuildContext context,
      bool needForceUpdate,
      String buildVersionNo,
      String buildVersion,
      String buildUpdateDescription,
      String buildName,
      String buildFileSize,
      String downloadURL,
      ) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) {
          return WillPopScope(
              child: TPUpdteWidget(
                needForceUpdate: needForceUpdate,
                // needForceUpdate: false,
                buildVersionNo: buildVersionNo,
                buildVersion: buildVersion,
                buildUpdateDescription: buildUpdateDescription,
                buildName: buildName,
                // buildFileSize: ((double.parse((buildFileSize??'0'))/1024/1024)?.toStringAsFixed(2)),
                // buildFileSize: 54.0265837*1024*1024,
                // buildFileSize: 50.0265837*1024*1024,
                // buildFileSize: 45.95*1024*1024,
                // buildFileSize: 56.7*1024*1024,
                buildFileSize: double.parse(buildFileSize),
                url: downloadURL,
                packageName: 'com.yangfann.smartminemgmg',
                width: MediaQuery.of(context).size.width*0.7,
                startInstallApk: ()=>Navigator.pop(context),
                onClose: ()=>_onCloseTap(context),
              ),
              onWillPop: ()=>Future(() => false));
        });

  }

  static _onCloseTap(BuildContext context) {
    print('点击关闭');
    Navigator.pop(context);
  }
}